<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :lg="4">
            <a-form-item label="品牌名">
              <a-input v-model="queryParam.name" placeholder="请输入品牌名称" :allowClear="true"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="4">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                  <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                  <a-button @click="handleAdd" type="primary" icon="plus" style="margin-left: 8px">新增</a-button>
                  <a-button type="primary" @click="resetSearch" icon="reload" style="margin-left: 8px">重置</a-button>
              </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="showStatus" slot-scope="text,record">
          <a-switch @change="changeStatus(record)" v-model="record.showStatus == 1" />
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="relateCategory(record)">关联分类</a>
          <a-divider type="vertical" />
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <pms-brand-modal ref="modalForm" @ok="modalFormOk"></pms-brand-modal>
    <brand-category-relation ref="relationModal"></brand-category-relation>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '../../utils/mixin'
import { JeecgListMixin } from '../../mixins/JeecgListMixin'
import PmsBrandModal from './modules/BrandModal.vue'
import { getAction } from '../../api/manage'
import ZSwitch from '../../components/compents/ZSwitch.vue'
import BrandCategoryRelation from './modules/BrandCategoryRelation.vue'

export default {
  name: 'PmsBrandList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    ZSwitch,
    PmsBrandModal,
    BrandCategoryRelation
  },
  data() {
    return {
      description: 'pms_brand管理页面',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '品牌名',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: 'logo',
          align: 'center',
          dataIndex: 'logo',
          scopedSlots: { customRender: 'imgSlot' }
        },
        {
          title: '介绍',
          align: 'center',
          dataIndex: 'description',
          customRender: function(text) {
            return text.length > 20 ? text.slice(0, 20) + '...' : text
          }
        },
        {
          title: '检索首字母',
          align: 'center',
          dataIndex: 'firstLetter',
          width: 20
        },
        {
          title: '显示状态',
          align: 'center',
          dataIndex: 'showStatus',
          scopedSlots: { customRender: 'showStatus' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 220,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/product/brand/list',
        delete: '/product/brand/delete',
        deleteBatch: '/product/brand/deleteBatch',
        changeStatus: '/product/brand/changeStatus'
      }
    }
  },
  created() {

  },
  computed: {},
  methods: {
    /**
     * 修改状态
     * @param record 当前对象
     */
    changeStatus(record) {
      getAction(this.url.changeStatus, { id: record.id }).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    /**
     * 重置搜索
     */
    resetSearch() {
      this.queryParam = {}
      this.loadData()
    },
    relateCategory(record) {
      this.$refs.relationModal.modalVisible = true
      this.$refs.relationModal.record = record
      this.$refs.relationModal.initRelationList(record)
    }
  }
}
</script>
<style scoped>
@import '/src/assets/less/common.less';
</style>